﻿#coding=utf-8
#Boa:Dialog:DlgProGX


import wx
import wx.grid
from wx.lib.anchors import LayoutAnchors

from product import *
from MyListCtrl import MyGridProductInventory

import kit

def create(parent):
    return DlgProGX(parent)

[wxID_DLGPROGX, wxID_DLGPROGXBTNCANCEL, wxID_DLGPROGXBTNOK, 
 wxID_DLGPROGXCHOICEUNIT, wxID_DLGPROGXTBRAND, wxID_DLGPROGXTCATEGORY, 
 wxID_DLGPROGXTCBRAND, wxID_DLGPROGXTCCATEGORY, wxID_DLGPROGXTCCNT, 
 wxID_DLGPROGXTCDEFSALE, wxID_DLGPROGXTCID, wxID_DLGPROGXTCKEYWORD, 
 wxID_DLGPROGXTCMEMO, wxID_DLGPROGXTCNAME, wxID_DLGPROGXTCNT, 
 wxID_DLGPROGXTCONHAND, wxID_DLGPROGXTCPRICE, wxID_DLGPROGXTCSPEC, 
 wxID_DLGPROGXTCTOTAL, wxID_DLGPROGXTDEFSALE, wxID_DLGPROGXTID, 
 wxID_DLGPROGXTKEYWORD, wxID_DLGPROGXTMEMO, wxID_DLGPROGXTNAME, 
 wxID_DLGPROGXTONHAND, wxID_DLGPROGXTPRICE, wxID_DLGPROGXTSPEC, 
 wxID_DLGPROGXTTOTAL, wxID_DLGPROGXTUNIT, 
] = [wx.NewId() for _init_ctrls in range(29)]

class DlgProGX(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGPROGX, name=u'DlgProGX',
              parent=prnt, pos=wx.Point(560, 183), size=wx.Size(507, 523),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u5546\u54c1\u4fe1\u606f')
        self.SetClientSize(wx.Size(499, 489))
        self.Center(wx.BOTH)
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u4eff\u5b8b_GB2312'))

        self.TID = wx.StaticText(id=wxID_DLGPROGXTID,
              label=u'\u7f16\u53f7\uff1a', name=u'TID', parent=self,
              pos=wx.Point(272, 32), size=wx.Size(56, 14), style=0)

        self.TCID = wx.StaticText(id=wxID_DLGPROGXTCID, label=u'...',
              name=u'TCID', parent=self, pos=wx.Point(344, 32), size=wx.Size(12,
              14), style=0)

        self.TName = wx.StaticText(id=wxID_DLGPROGXTNAME,
              label=u'\u540d\u79f0\uff1a', name=u'TName', parent=self,
              pos=wx.Point(56, 32), size=wx.Size(56, 14), style=0)

        self.TCName = wx.StaticText(id=wxID_DLGPROGXTCNAME, label=u'...',
              name=u'TCName', parent=self, pos=wx.Point(128, 32),
              size=wx.Size(12, 14), style=0)

        self.TSpec = wx.StaticText(id=wxID_DLGPROGXTSPEC,
              label=u'\u89c4\u683c\uff1a', name=u'TSpec', parent=self,
              pos=wx.Point(56, 64), size=wx.Size(56, 14), style=0)

        self.TCSpec = wx.StaticText(id=wxID_DLGPROGXTCSPEC, label=u'...',
              name=u'TCSpec', parent=self, pos=wx.Point(128, 64),
              size=wx.Size(12, 14), style=0)

        self.TBrand = wx.StaticText(id=wxID_DLGPROGXTBRAND,
              label=u'\u54c1\u724c\uff1a', name=u'TBrand', parent=self,
              pos=wx.Point(56, 96), size=wx.Size(56, 14), style=0)

        self.TCBrand = wx.StaticText(id=wxID_DLGPROGXTCBRAND, label=u'...',
              name=u'TCBrand', parent=self, pos=wx.Point(128, 96),
              size=wx.Size(12, 14), style=0)

        self.TUnit = wx.StaticText(id=wxID_DLGPROGXTUNIT,
              label=u'\u5355\u4f4d\uff1a', name=u'TUnit', parent=self,
              pos=wx.Point(56, 200), size=wx.Size(56, 14), style=0)

        self.choiceUnit = wx.Choice(choices=[], id=wxID_DLGPROGXCHOICEUNIT,
              name=u'choiceUnit', parent=self, pos=wx.Point(128, 192),
              size=wx.Size(72, 24), style=0)
        self.choiceUnit.Bind(wx.EVT_CHOICE, self.OnChoiceUnitChoice,
              id=wxID_DLGPROGXCHOICEUNIT)

        self.TOnhand = wx.StaticText(id=wxID_DLGPROGXTONHAND,
              label=u'\u5e93\u5b58\uff1a', name=u'TOnhand', parent=self,
              pos=wx.Point(272, 96), size=wx.Size(56, 14), style=0)

        self.TCOnhand = wx.StaticText(id=wxID_DLGPROGXTCONHAND, label=u'...',
              name=u'TCOnhand', parent=self, pos=wx.Point(344, 96),
              size=wx.Size(12, 14), style=0)

        self.TMemo = wx.StaticText(id=wxID_DLGPROGXTMEMO,
              label=u'\u5907\u6ce8\uff1a', name=u'TMemo', parent=self,
              pos=wx.Point(56, 160), size=wx.Size(56, 14), style=0)

        self.TCMemo = wx.StaticText(id=wxID_DLGPROGXTCMEMO, label=u'...',
              name=u'TCMemo', parent=self, pos=wx.Point(128, 160),
              size=wx.Size(12, 14), style=0)

        self.TCategory = wx.StaticText(id=wxID_DLGPROGXTCATEGORY,
              label=u'\u7c7b\u578b\uff1a', name=u'TCategory', parent=self,
              pos=wx.Point(272, 64), size=wx.Size(56, 14), style=0)

        self.TCCategory = wx.StaticText(id=wxID_DLGPROGXTCCATEGORY,
              label=u'...', name=u'TCCategory', parent=self, pos=wx.Point(344,
              64), size=wx.Size(12, 14), style=0)

        self.TKeyWord = wx.StaticText(id=wxID_DLGPROGXTKEYWORD,
              label=u'\u6807\u7b7e\uff1a', name=u'TKeyWord', parent=self,
              pos=wx.Point(56, 128), size=wx.Size(51, 16), style=0)

        self.TCKeyWord = wx.StaticText(id=wxID_DLGPROGXTCKEYWORD, label=u'...',
              name=u'TCKeyWord', parent=self, pos=wx.Point(128, 128),
              size=wx.Size(12, 14), style=0)

        self.TDefsale = wx.StaticText(id=wxID_DLGPROGXTDEFSALE,
              label=u'\u9884\u8bbe\u552e\u4ef7\uff1a', name=u'TDefsale',
              parent=self, pos=wx.Point(272, 200), size=wx.Size(88, 14),
              style=0)

        self.TCDefSale = wx.StaticText(id=wxID_DLGPROGXTCDEFSALE, label=u'0.0',
              name=u'TCDefSale', parent=self, pos=wx.Point(360, 200),
              size=wx.Size(27, 16), style=0)

        self.TCnt = wx.StaticText(id=wxID_DLGPROGXTCNT,
              label=u'\u6570\u91cf\uff1a', name=u'TCnt', parent=self,
              pos=wx.Point(56, 240), size=wx.Size(64, 14), style=0)

        self.TCCnt = wx.TextCtrl(id=wxID_DLGPROGXTCCNT, name=u'TCCnt',
              parent=self, pos=wx.Point(128, 232), size=wx.Size(88, 22),
              style=0, value=u'0')
        self.TCCnt.SetAutoLayout(False)
        self.TCCnt.SetHelpText(u'')
        self.TCCnt.SetToolTipString(u'TCCnt')
        self.TCCnt.Bind(wx.EVT_TEXT, self.OnTCCntText, id=wxID_DLGPROGXTCCNT)

        self.TPrice = wx.StaticText(id=wxID_DLGPROGXTPRICE,
              label=u'\u5355    \u4ef7\uff1a', name=u'TPrice', parent=self,
              pos=wx.Point(272, 240), size=wx.Size(87, 16), style=0)

        self.TCPrice = wx.TextCtrl(id=wxID_DLGPROGXTCPRICE, name=u'TCPrice',
              parent=self, pos=wx.Point(360, 232), size=wx.Size(100, 22),
              style=0, value=u'0.0')

        self.TTotal = wx.StaticText(id=wxID_DLGPROGXTTOTAL,
              label=u'\u5c0f\u8ba1\uff1a', name=u'TTotal', parent=self,
              pos=wx.Point(272, 272), size=wx.Size(64, 14), style=0)

        self.TCTotal = wx.StaticText(id=wxID_DLGPROGXTCTOTAL, label=u'0.0',
              name=u'TCTotal', parent=self, pos=wx.Point(360, 272),
              size=wx.Size(27, 16), style=0)

        self.btnOK = wx.Button(id=wxID_DLGPROGXBTNOK, label=u'\u786e\u5b9a',
              name=u'btnOK', parent=self, pos=wx.Point(120, 432),
              size=wx.Size(75, 24), style=0)
        self.btnOK.Bind(wx.EVT_BUTTON, self.OnBtnOKButton,
              id=wxID_DLGPROGXBTNOK)

        self.btnCancel = wx.Button(id=wxID_DLGPROGXBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(256, 432), size=wx.Size(75, 24), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGPROGXBTNCANCEL)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.myViewInit()
        
        
    def myViewInit(self):
        self.grid = MyGridProductInventory(parent=self, pos=wx.Point(64, 304), size=wx.Size(400, 100),
                    style=wx.SUNKEN_BORDER)
        self.grid.doMyInit()
        
        self.setInbound()
        
    def unitIsNull(self, unit):
        return (unit is None) or (unit == '')
    
    def setInbound(self, inbound=True):
        self.inbound = inbound
        
    def setData(self, obj=None, proId=-1, unit='', cnt=0.0, price=0.0):
        if obj is None:
            if proId == -1:
                print '[Error]undefined data'
                return
            obj = CDataProduct.getData({'id':proId})
            if obj is None:
                print '[Error]undefined id %d' %proId
                return
        
        # 150430 - 设置grid单位数据
        self.grid.attachProduct(obj)
        
        self.data = obj
        #print type(obj.m_fieldVals[CDataProduct.getFieldNo(u'name')])
        self.TCID.SetLabel(obj.m_fieldVals[CDataProduct.getFieldNo(u'id')].__str__())
        self.TCName.SetLabel(obj.m_fieldVals[CDataProduct.getFieldNo(u'name')])
        self.TCSpec.SetLabel(obj.m_fieldVals[CDataProduct.getFieldNo(u'spec')])
        
        brand = obj.m_fieldVals[CDataProduct.getFieldNo(u'brand')]
        if (brand is None):
            brand = ''
        self.TCBrand.SetLabel(brand)

        unit1 = obj.m_fieldVals[CDataProduct.getFieldNo(u'unit1')]
        unit2 = obj.m_fieldVals[CDataProduct.getFieldNo(u'unit2')]
        unit3 = obj.m_fieldVals[CDataProduct.getFieldNo(u'unit3')]
##        print unit1
        if(self.unitIsNull(unit1)):
            print '[ERROR] Null Unit1'
        else:
            self.choiceUnit.Append(unit1) 
               
        if not (self.unitIsNull(unit2)):
            self.choiceUnit.Append(unit2)
            if not (self.unitIsNull(unit3)):
                self.choiceUnit.Append(unit3)
        
        defUnitNo = 0
        if (unit != ''):
            if (unit1 == unit):
                defUnitNo = 0
            elif (unit2 == unit):
                defUnitNo = 1
            elif (unit3 == unit):
                defUnitNo = 2
            else:
                print '[Error] undefined unit <%s>' %(unit)    
        self.choiceUnit.SetSelection(defUnitNo)

        self.TCOnhand.SetLabel(obj.m_fieldVals[CDataProduct.getFieldNo(u'onhand1')].__str__())

        self.TCMemo.SetLabel(obj.m_fieldVals[CDataProduct.getFieldNo(u'memo')])

        categoryId = obj.getFieldVal(index=CDataProduct.COL_CATEGORYID)
        if (categoryId is None):
            cateogry = ''
        else:
            cateogry = CDataCategory.getName(categoryId)
        self.TCCategory.SetLabel(cateogry)

        #self.TCKeyWord.SetLabel(obj.m_fieldVals[CDataProduct.getFieldNo(u'brand')]) 
        defSale = obj.m_fieldVals[CDataProduct.getFieldNo(u'defSale1')]
        if defSale is None:
            defSale = 0.0
        self.TCDefSale.SetLabel(defSale.__str__())

        # 采购模式下不应该使用售价，目前简单起见，为0需手动修改
        #if(price == 0.0):
        #    price = defSale
        self.TCPrice.SetValue(price.__str__())
        
        self.TCCnt.SetValue(cnt.__str__())
        print '%s %s, %s %s' %(type(price),price, type(cnt), cnt)
        #print '%s %s %s %s' %(price == 'None'.__str__(), price != None, price is None, cnt is None)
##        if (price == 'None' or cnt == 'None'):
##            self.TCTotal.SetLabel('0.0')            
##        else:
##            self.TCTotal.SetLabel(str(float(price)*float(cnt)))
        total = float(price) * cnt
        self.TCTotal.SetLabel(str(total))

        #Set TCCnt focus
        self.TCCnt.SetInsertionPointEnd()
        self.TCCnt.SetSelection(-1, -1)
        self.TCCnt.SetFocus()
        
    def OnBtnOKButton(self, event):
        #print self.choiceUnit.GetString(self.choiceUnit.GetSelection())
        #print type(self.choiceUnit.GetString(self.choiceUnit.GetSelection()))
        
        self.unit = self.choiceUnit.GetString(self.choiceUnit.GetSelection())
        self.cnt = float(self.TCCnt.GetValue().__str__())
        self.price = float(self.TCPrice.GetValue().__str__())
        self.lonelyUnit = self.grid.getLonelyUnit(self.unit)
        #self.SetReturnCode(wx.ID_OK)
        #self.SetReturnCode(100)
        self.EndModal(wx.ID_OK)
        #self.Close()
        event.Skip()

    def OnBtnCancelButton(self, event):
        #self.SetReturnCode(wx.ID_CANCEL)
        #self.Close()
        self.EndModal(wx.ID_CANCEL)
        event.Skip()

    def syncGrid(self):
        if not self.TCCnt.GetValue() or self.TCCnt.GetValue() == '-':
            return
        
        try:
            cnt = float(self.TCCnt.GetValue())
        except ValueError as exp:
            kit.showExcept(exp, u'输入错误：请输入数值')
        else:
            unit = self.choiceUnit.GetString(self.choiceUnit.GetSelection())
            self.grid.adjustInventory(unit, cnt if self.inbound else -cnt)
        
    def OnChoiceUnitChoice(self, event):
        self.syncGrid()

    def OnTCCntText(self, event):
        self.syncGrid()
            
            
